package executors.completable.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThenCombineTest001 {
  public static void main(String[] args) {
    ExecutorService executors = Executors.newSingleThreadExecutor();
    
    CompletableFuture<String> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
      System.out.println("这是执行A");
      try {
        TimeUnit.SECONDS.sleep(3);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      return "a";
    }, executors);
    
    uCompletableFuture.thenCombineAsync(CompletableFuture.supplyAsync(() -> {
      System.out.println("这是执行B");
      try {
        TimeUnit.SECONDS.sleep(2);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      return "b";
    }, executors), (res1, res2) -> {
      System.out.println("执行计划A AND B之后，开始执行此方法");
      return res2 + res1;
    });
  }
}
